package com.yuyue.note01.controller;

import com.yuyue.note01.entity.Account;
import com.yuyue.note01.entity.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 在类上使用 @RequestMapping 对其下所有路径映射生效
 */
@Controller
@RequestMapping("/classCotroller")
public class MappingController {

    //配合验证类上的 @RequestMapping
    @RequestMapping("show")
    @ResponseBody
    public String show(){
        System.out.println("在类上使用 @RequestMapping 对其下所有路径映射生效");
        return "{'msg' : '在类上使用 @RequestMapping 对其下所有路径映射生效'}";
    }

    //获取 get 方式请求的参数
    @RequestMapping("getParam")
    @ResponseBody
    //方法的参数名称直接取get方式请求时的参数名称
    public String getParam(String name, String description){
        System.out.println(name);
        System.out.println(description);
        return "{'msg' : '收到！'}";
    }

    //获取 post 方式请求的产生
    @RequestMapping("getPost")
    @ResponseBody
    public String getPost(String name, String description){
        System.out.println(name);
        System.out.println(description);
        return "{'msg' : '收到！'}";
    }

    //设置到对象相同名称的属性中
    @RequestMapping("pojoParam")
    @ResponseBody
    public String pojoParam(User user){
        System.out.println(user);
        return "{'Name' : " + user.getName() +" }";
    }

    //设置到含有对象的对象中
    @RequestMapping("pojoContainPojoParam")
    @ResponseBody
    public String pojoContainPojoParam(Account account){
        System.out.println(account);
        return "收到！";
    }

    //设置到数组中
    @RequestMapping("arrayParam")
    @ResponseBody
    public String arrayParam(String[] likes){
        System.out.println(Arrays.asList(likes));
        return "收到！";
    }

    @RequestMapping("getParamForDifferentName")
    @ResponseBody
    //请求参数名称与方法参数名称不一致时,获取参数失败，使用@RequestParam绑定参数
    //public String getParamForDifferentName(String newName, String description){
    public String getParamForDifferentName(@RequestParam("name") String newName, String description){
        return "{'newName' : " + newName +" }";
    }

    //设置到集合中
    @RequestMapping("listParam")
    @ResponseBody
    //List、ArrayList 类型，spring会尝试创建对象，但因为集合中的类型未指定，导致设置值失败
    //ArrayList<String> 类型，未报错，值为空
    //使用 @RequestParam 绑定参数
    public String listParam(@RequestParam List list){
        System.out.println(list);
        return "收到！";
    }

    //接收json格式数据
    @RequestMapping("listParamForJson")
    @ResponseBody
    public String listParamForJson(@RequestBody List<String> list){
        System.out.println(list);
        return "收到！";
    }

    //传递json的对象
    @RequestMapping("pojoParamForJson")
    @ResponseBody
    public String pojoParamForJson(@RequestBody User user){
        System.out.println(user);
        return "收到！";
    }

    //传递json的对象集合
    @RequestMapping("listPojoParamForJson")
    @ResponseBody
    public String listPojoParamForJson(@RequestBody List<Account> accounts){
        System.out.println(accounts);
        return "收到！";
    }

    //时间格式转换
    @RequestMapping("dataParam")
    @ResponseBody
    public String dataParam(Date date,
                            @DateTimeFormat(pattern = "yyyy-MM-dd") Date date1,
                            @DateTimeFormat(pattern = "yyyy/MM/dd HH:mm:ss") Date date2){
        System.out.println("默认 date(yyyy/mm/dd) ==> "+date);
        System.out.println("转换 date(yyyy-MM-dd) ==> "+date1);
        System.out.println("转换 date(yyyy/MM/dd HH:mm:ss) ==> "+date2);

        return "收到！";
    }

    /*---------------- 返回信息 ----------------*/
    //跳转页面，返回路径直接跳转，下层文件需要 / 开头
    @RequestMapping("holloMVC")
    public String holloMVC(){
        return "/WEB-INF/note01/HolloMVC.jsp";
    }

    //不解析，返回字符串信息
    @RequestMapping("responseString")
    @ResponseBody
    public String responseString(){
        return "这个字符串要展示在页面上！";
    }

    //返回 json 对象
    @RequestMapping("toJsonPOJO")
    @ResponseBody
    public User toJsonPOJO(){
        User user = new User();
        user.setName("诸葛青");
        user.setDescription("不听八卦");
        return user;
    }

    //返回 json 对象集合
    @RequestMapping("toJsonList")
    @ResponseBody
    public List<User> toJsonList(){
        List<User> list = new ArrayList<User>();
        User user1 = new User();
        user1.setName("张楚岚");
        user1.setDescription("不摇碧莲");
        list.add(user1);

        User user2 = new User();
        user2.setName("冯宝宝");
        user2.setDescription("机智一匹");
        list.add(user2);

        return list;
    }

}
